package com.gantang.poc.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Callable;

@Slf4j
public class Api3Task implements Callable<Boolean> {
    private final RestTemplate restTemplate;
    private final String url;
    private final Object requestBody;

    public Api3Task(RestTemplate restTemplate, String url, Object requestBody) {
        this.restTemplate = restTemplate;
        this.url = url;
        this.requestBody = requestBody;
    }

    @Override
    public Boolean call() {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
            log.info("Task executed with response: " + response.getBody());
            return response.getStatusCode().is2xxSuccessful();
        } catch (Exception e) {
            log.error("Error executing task", e);
            return false;
        }
    }
}